import { TYPES } from '../reduces/rewardReducer';
import Http from '../../../utils/http';
import _ from 'lodash';
import {
  Toast,
} from 'antd-mobile-rn';
import { i18n } from '../../../i18n/index';

export const getRewardList = () => async dispatch => {
  try {
    const http = new Http();

    dispatch({ type: TYPES.GET_REWARD_LIST });

    const { status, data } = await http.post("rewardlist");

    if (status) {
      dispatch({ type: TYPES.GET_REWARD_LIST_SUCCESS, rewards: data.rewards });
    } else {
      throw new Exception();
    }
  } catch (_) {
    dispatch({ type: TYPES.GET_REWARD_LIST_FAIL });

    Toast.offline(i18n.t("office.reward.error.getrewardlist"), 1);
  }
};

export const saveReward = ({reward, image}) => async dispatch => {
  try {
    const http = new Http();

    dispatch({ type: TYPES.SAVE_REWARD });

    let newReward = { ...reward };
    let isAdded = true;
    if (_.has(newReward, 'server_reward_id')) {
      newReward.reward_id = newReward.server_reward_id;
      delete newReward['server_reward_id'];
      isAdded = false;
    }

    newReward.take_points = parseInt(newReward.take_points);
    newReward.sort_order = parseInt(newReward.sort_order);

    // Use base64 to upload new image.
    if (!_.isEmpty(image)) {
      newReward.image = `data:${image.mime};base64,`+ image.data;
    }

    const { status, data } = await http.post("rewardsave", newReward);

    if (status) {
      const { reward_id } = data;
      newReward.reward_id = reward_id;

      // Use local image path to update view quickly
      if (!_.isEmpty(image)) {
        newReward.image = image.path;
      }

      dispatch({ type: TYPES.SAVE_REWARD_SUCCESS, reward: newReward, is_new: isAdded });
    } else {
      throw new Exception();
    }
  } catch (_) {
    dispatch({ type: TYPES.SAVE_REWARD_FAIL });

    Toast.offline(i18n.t("office.reward.error.savereward"), 1);
  }
};

export const deleteReward = (rewardId) => async dispatch => {
  try {
    const http = new Http();

    dispatch({ type: TYPES.DELETE_REWARD });

    const { status, data } = await http.post("rewarddelete", { reward_id:rewardId });

    if (status) {
      dispatch({ type: TYPES.DELETE_REWARD_SUCCESS, reward_id: rewardId });
    } else {
      throw new Exception();
    }
  } catch (_) {
    dispatch({ type: TYPES.DELETE_REWARD_FAIL });

    Toast.offline(i18n.t("office.reward.error.deletereward"), 1);
  }
};

export const getRewardProducts = (rewardId = false) => async dispatch => {
  try {
    const http = new Http();

    dispatch({ type: TYPES.GET_REWARD_PRODUCTS });

    const postData = rewardId ? { reward_id: rewardId } : {};
    const { status, data } = await http.post("rewarddetail", postData);

    if (status) {
      dispatch({ type: TYPES.GET_REWARD_PRODUCTS_SUCCESS, products: data.products });
    } else {
      dispatch({ type: TYPES.GET_REWARD_PRODUCTS_FAIL });
    }
  } catch (_) {
    dispatch({ type: TYPES.GET_REWARD_DETAIL_FAIL });
  }
};

export const addNewReward = () => {
  return {
    type: TYPES.ADD_NEW_REWARD,
  };
}

export const editReward = (reward) => {
  return {
    type: TYPES.EDIT_EXISTING_REWARD,
    reward,
  };
}

export const cancelEditReward = () => {
  return {
    type: TYPES.CANCEL_EDIT_REWARD,
  };
}